<template>
  <div>
    <div class="toolbar">
      <el-form :inline="true" :model="q" size="mini">
        <el-form-item label="用户ID:">
          <el-input v-model="q.player_id" placeholder="用户ID" prefix-icon="el-icon-edit"></el-input>
        </el-form-item>
        <el-form-item label="单号:">
          <el-input v-model="q.data_id" placeholder="订单号" prefix-icon="el-icon-edit"></el-input>
        </el-form-item>
        <el-form-item label="提交时间:">
          <el-date-picker v-model="q.created_at_start" value-format="yyyy-MM-dd HH:mm:ss" type="datetime"
            placeholder="选择日期时间"></el-date-picker>
          <span>-</span>
          <el-date-picker v-model="q.created_at_end" value-format="yyyy-MM-dd HH:mm:ss" type="datetime"
            placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="问题类型：">
          <el-select v-model="q.reset_type_id" clearable placeholder="请选择">
            <el-option v-for="item in problemsTypeList" :key="item.id" :label="item.desc" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态：">
          <el-select v-model="q.status" clearable placeholder="请选择">
            <el-option v-for="item in this.$store.state.app.statusList" :key="item.id" :label="item.text"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户等级：">
          <el-select v-model="q.level" clearable placeholder="请选择">
            <el-option v-for="item in [0,1,2,3,4,5,6,7,8,9,10]" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
          <el-button type="danger" @click="reload">重置|刷新</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row type="flex" class="row-bg btn-row" justify="end">
      <el-button size="mini" @click="receiptWork('work')" :disabled="working" :type="working?'success':'info'"
        icon="el-icon-circle-check">{{working ?'接单中':'开始接单'}}
      </el-button>
      <el-button size="mini" @click="receiptWork('stop')" :disabled="!working" :type="!working?'success':'info'"
        icon="el-icon-circle-close">{{working ?'停止接单':'休息中'}}
      </el-button>
    </el-row>
    <div style="text-align: center;font-size: 14px;line-height: 14px;">
      <p>今日已处理: {{countNum.finish_count}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当前待处理: <span
          style="color: red">{{countNum.wait_count}}</span></p>
    </div>
    <el-table class="el-calendar-table" :data="listData.items" v-loading.body="listLoading" size="mini" stripe border
      fit highlight-current-row @selection-change="handleSelectionChange" @sort-change="sortlist">
      <el-table-column prop="id" label="单号" width="80">
      </el-table-column>
      <el-table-column prop="player_id" label="用户ID" width="100">
      </el-table-column>
      <el-table-column prop="type.desc" label="问题类型" width="100">
      </el-table-column>
      <el-table-column prop="reset_type.desc" label="重设问题类型" width="100">
      </el-table-column>
      <el-table-column prop="level" label="用户等级" width="100">
      </el-table-column>
      <el-table-column min-width="160" prop="created_at" label="提交时间" sortable>
      </el-table-column>
      <el-table-column width="100" prop="status" label="处理状态">
        <template slot-scope="scope">
          <span>{{scope.row.status === 0?'未处理':'已处理'}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="160" prop="finish_at" label="回复时间">
      </el-table-column>
      <el-table-column width="100" prop="process_time" label="处理时长(s)">
        <template slot-scope="scope">
        <span v-if="scope.row.process_time">{{scope.row.process_time.toFixed()}}</span><span v-else>0</span>
        </template>
      </el-table-column>
      <!-- <el-table-column width="100" prop="is_solved" label="是否解决">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 0"></span>
          <span v-else>{{scope.row.is_solved === null || scope.row.is_solved === 0?'未解决':'已解决'}}</span>
        </template>
      </el-table-column> -->
      <el-table-column width="100" prop="urgent_count" label="加急次数">
      </el-table-column>
      <el-table-column width="210" prop="handler" label="操作人">
      </el-table-column>
      <el-table-column label="操作" width="80" fixed="right">
        <template slot-scope="scope">
          <div>
            <el-button v-if="scope.row.status === 1" type="primary" size="mini" @click="Details(scope.row)">查看
            </el-button>
            <el-button v-else type="success" size="mini" @click="Details(scope.row)">回复</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="q.page_no"
        :page-sizes="$store.state.app.pageSizeArr" :page-size="q.page_size"
        layout="total, sizes, prev, pager, next, jumper" :total="listData.total">
      </el-pagination>
    </div>
    <save-update v-dialogDrag v-if="showUpdate" :showUpdate.sync="showUpdate" :id="show_id" :problemsTypeList="problemsTypeList"
      :status="status"></save-update>
  </div>
</template>

<script>
import { getCustomerOrder, stopOrder, workingOrder, getProblemsList, getCustomerOrderCount } from '@/api'
import { queryFilter } from '@/utils/patch'
import saveUpdate from './saveUpdate'

export default {
  name: "index",
  components: { saveUpdate },
  data () {
    return {
      show_id: null,
      status: null,
      listData: {},
      q: { page_size: 30, page_no: 1 },
      listLoading: false,
      showUpdate: false,
      problemsTypeList: [],
      Timetype: true,
      working: '',
      countNum: {},
      timer: null
    }
  },
  created () {
    getProblemsList().then(res => {
      if (res.status === 0) {
        this.problemsTypeList = res.data.items
        console.log('prob',this.problemsTypeList)
      }
    })
    this.getList()
    this.getCustomerOrderCount()
    this.timerStart()
  },
  mounted () {
  },
  methods: {
    sortlist(sort) {
      if (sort.order === "descending") {
        this.q.order_by = "-" + sort.prop;
      } else {
        this.q.order_by = sort.prop;
      }
      this.getList();
    },
    receiptWork (val) {
      //通过val值判断点击的是工作还是停止
      //如果是工作，working改为true。否则为false
      if (val === 'work') {
        // this.working = true
        workingOrder().then((res) => {
          if (res.status === 0) {
            this.$message({ type: 'success', message: '已开始接单' })
            this.working = true
          }
        })
      } else {
        stopOrder().then((res) => {
          if (res.status === 0) {
            if (res.data.wait_count===0){
              this.$message({ type: 'warning', message: '已停止接单' })
            }else{
              this.$message({ showClose: true, duration: 0, type: 'warning', message: '您当前有'+res.data.wait_count+"笔未处理订单,请通知主管转接" })
            }
            this.working = false
          }
        })
      }
      this.$store.dispatch("UPDATEUSERSTATUS")
    },
    Details (row) {
      this.show_id = row.id
      this.status = row.status
      this.showUpdate = true
      this.timerEnd()
    },
    sendMsg () {

    },
    query () {
      this.q = Object.assign(this.q, { page_no: 1, page_size: 30 })
      this.getCustomerOrderCount()
      this.getList()
    },
    reload () {
      this.q = { page_size: 30, page_no: 1 }
      this.getList()
      this.getCustomerOrderCount()
      this.timerEnd()
      this.timerStart()
    },
    handleSizeChange (val) {
      this.q.page_size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.q.page_no = val
      this.getList()
    },

    getList () {
      this.q = queryFilter(this.q)
      this.listLoading = true
      getCustomerOrder(this.q).then(res => {
        console.log('==L',res)
        if (res.status === 0) {
          this.listLoading = false
          this.listData = res.data
        }
      })
    },
    handleSelectionChange (sel) {
      console.log('s',sel)
    },
    toggleTimetype () {
      if (this.Timetype) {
        delete this.q.created_at_begin
        delete this.q.created_at_end
        this.Timetype = !this.Timetype
      } else {
        delete this.q.updated_at_begin
        delete this.q.updated_at_end
        this.Timetype = !this.Timetype
      }
    },
    getCustomerOrderCount () {
      this.countNum.finish_count = '-'
      this.countNum.wait_count = '-'
      getCustomerOrderCount().then(res => {
        console.log('===c',res)
        if (res.status === 0) {
          this.countNum = res.data
          this.working = res.data.status === 0 ? false : true;
        }
      })
    },
    timerStart () {
      this.timer = setInterval(() => {
        this.getList()
        this.getCustomerOrderCount()
      }, 60000)
    },
    timerEnd () {
      clearInterval(this.timer)
    }
  },
  watch: {
    showUpdate () {
      if (this.showUpdate !== 0 && !this.showUpdate) {
        this.getList()
      }
    }
  },
  destroyed() {
    this.timerEnd()
  }
}
</script>

<style scoped>
.el-form-item__label {
  width: 100px !important;
}
</style>
